
const SMACK_TINYURL_ENABLE = "extensions.swaksmack.tinyurl_enable_boolpref1";
const SMACK_TINYURL_THRESHOLD = "extensions.swaksmack.tinyurl_maxurllength_intpref";

const SS_chatPostRegexStr = "(http:\\/\\/[^\"'\\s>]*\\/?([^\"'\\s>]*)?)";
const SS_chatPostRegEx = new RegExp(SS_chatPostRegexStr, "ig");

var SS_literals = /(\/|\.|\^|\$|\*|\+|\?|\||\(|\)|\[|\]|\{|\}|\\)/g;

var SS_charUIDChoices = "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWZYZ_";
 
 String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function SS_getStringTableText(key) {
	var jellyfishstrings = document.getElementById("swaksmack-strings");
	return jellyfishstrings.getString(key);
}

function SS_getPreference(pref_name)
{
  var pref_type = SS_prefs.getPrefType(pref_name);
  if (pref_type == SS_prefs.PREF_STRING)
      return SS_prefs.getCharPref(pref_name);
  else if (pref_type == SS_prefs.PREF_INT)
      return SS_prefs.getIntPref(pref_name);
  else if (pref_type == SS_prefs.PREF_BOOL)
      return SS_prefs.getBoolPref(pref_name);
  else  // fallback on error
    return SS_prefs.PREF_INVALID;
}

function SS_getCharUID(generateNew) {
	if (generateNew == null) generateNew = false;
	if (SS_CHARUID == null || generateNew) {
		var temp = '';
		var size_of_uid = 13;
		for (var i=0; i<size_of_uid; i++) {
  		var rnd = Math.floor(Math.random() * SS_charUIDChoices.length);
  		temp += SS_charUIDChoices.substring(rnd,rnd+1);
		}
		SS_CHARUID = temp;

	} 
	return SS_CHARUID;
}


function SS_getChatPostUrlText(postText) {
	var matches	= null;
	var results = new Array();
	var numMatch = 0;
	while ((matches = SS_chatPostRegEx.exec(postText)) != null) {
		//check if url end with :.!, and remove
		//alert('matches[1]' + matches[1]);
 		var newurl = matches[1].replace(/[\.!,;:]$/i,'');
 		results[numMatch++] = newurl;
	}
	
 	return results;
}

//converts regex literals characters in the parameter 
function SS_escapeRegExLiterals(plaintext) {
	if (plaintext == null) return null;
	var converted = plaintext.replace(SS_literals,"\\$1");
	return converted
}

function openOptions() {
	alert('openOptions');
	    window.openDialog(
        "chrome://swaksmack/content/options.xul", 
        "options",
        "chrome, dialog, centerscreen");
}